package com.ww.learn.coroutine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val airportCodes = listOf<String>("LAX", "SF-", "PD-", "SEA")

    val airportData = airportCodes.map { anAirportCode ->
        async(Dispatchers.IO + SupervisorJob()) {
            Airport.getAirportData(anAirportCode)
        }
    }

    for (anAirportData in airportData) {
        try {
            val airport = anAirportData.await()
            println("${airport?.code} ${airport?.delay}")
        } catch (ex: Exception) {
            println("Error: ${ex.message?.substring(0..10)}")
        }
    }
}